<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script crossorigin="anonymous"
        integrity="sha512-t8vdA86yKUE154D1VlNn78JbDkjv3HxdK/0MJDMBUXUjshuzRgET0ERp/0MAgYS+8YD9YmFwnz6+FWLz1gRZaw=="
        src="https://lib.baomitu.com/crypto-js/4.1.1/core.min.js"></script>
    <script crossorigin="anonymous"
        integrity="sha512-uvzp6SWAgwnosfQBgY3a0lfCnQcduEufd+gxopKBBaSQrV1uaz5dW1/kzyMV63BvgA8RqUvMlK2G75n89ofFiQ=="
        src="https://lib.baomitu.com/crypto-js/4.1.1/cipher-core.js"></script>
        <script crossorigin="anonymous" integrity="sha512-2URa8a/+h6+6ovpCS/un8vJSHdSqJXS6jl4OCuV485PEB0mvFLoWSRSJYDk/K21KiRJ4YOBgDdf2vdaXkUksNA==" src="https://lib.baomitu.com/crypto-js/4.1.1/aes.js"></script>
        <script crossorigin="anonymous" integrity="sha512-IKpu1GFk/bQ+2fOc4sXuA6lm7Rct4P7611iDBxY9LReOZ2PpwoDWWqj6GSYejUce1FLxo/d4lxAnKqGWJuBw7g==" src="https://lib.baomitu.com/crypto-js/4.1.1/enc-base64.min.js"></script>
        <script crossorigin="anonymous" integrity="sha512-nCLs9aDWGaGY4FWp4aKTiurwVOWdVQ36QP2kMSYn1XmaHPNOcho4p6FlrzhoBSIdZHySyNCfR7T50ewCc+2hNg==" src="https://lib.baomitu.com/crypto-js/4.1.1/enc-base64url.js"></script>
        <script crossorigin="anonymous" integrity="sha512-NQVmLzNy4Lr5QTrmXvq/WzTMUnRHmv7nyIT/M6LyGPBS+TIeRxZ+YQaqWxjpRpvRMQSuYPQURZz/+pLi81xXeA==" src="https://lib.baomitu.com/crypto-js/4.1.1/crypto-js.js"></script>
</head>

<body>
    <script>
        let json = { "birthday": "1990-03-07", "tel": "17020702086", "sex": 2, "cname": "潘小芬", "doctype": 1, "idcard": "500101199003072642", "mxid": "s2KWASW0AABUjDQB", "date": "2022-10-12", "pid": "KgIAAA", "Ftime": 1, "guid": "" };
        json = JSON.stringify(json);
        const key = CryptoJS.enc.Utf8.parse("d779506dc877d686"); //16位
        const iv = CryptoJS.enc.Utf8.parse("1234567890000000");
        console.log(CryptoJS);
        //aes加密
        const encrypt = function (word) {
            let encrypted = "";
            if (typeof word == "string") {
                const srcs = CryptoJS.enc.Utf8.parse(word);
                encrypted = CryptoJS.AES.encrypt(srcs, key, {
                    iv: iv,
                    mode: CryptoJS.mode.CBC,
                    padding: CryptoJS.pad.Pkcs7
                });
            } else if (typeof word == "object") {
                //对象格式的转成json字符串
                const data = JSON.stringify(word);
                const srcs = CryptoJS.enc.Utf8.parse(data);
                encrypted = CryptoJS.AES.encrypt(srcs, key, {
                    iv: iv,
                    mode: CryptoJS.mode.CBC,
                    padding: CryptoJS.pad.Pkcs7
                });
            }
            return encrypted.ciphertext.toString();
        };
        // aes解密
        const decrypt = function (word) {
            const encryptedHexStr = CryptoJS.enc.Hex.parse(word);
            const srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
            const decrypt = CryptoJS.AES.decrypt(srcs, key, {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            });
            const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
            return decryptedStr.toString();
        }
        json = encrypt(json)
        console.log('加密: ' + json);
        json = decrypt(json)
        console.log('解密: ' + json);
    </script>
</body>

</html>